<?php
/*
Plugin Name: 哀悼
Plugin URI: https://www.beizigen.com/post/wordpress-one-button-all-station-black-and-white-plug-in-mourning/
Description: 哀悼那个4小时前坠楼的孩子，以及疫情中的逝者。
Version: 1.0.0
Author: 背字根
Author URI: https://www.beizigen.com/
*/

function mourning_mourning()
{
	$switch = get_option('mourning_switch');
	if ('on' != $switch) return;

	$start = strtotime(get_option('mourning_start'));
	$end = strtotime(get_option('mourning_end'));
	$offet = get_option('gmt_offset');
	$now = strtotime(wp_date('Y-m-d'));
	if ($start <= $now && $end >= $now) {
		echo "<style type=\"text/css\">
			html {
				-webkit-filter: grayscale(100%);
				-moz-filter: grayscale(100%);
				-ms-filter: grayscale(100%);
				-o-filter: grayscale(100%);
				filter: grayscale(100%);
				filter:gray;
			}
			</style>";
	}
}
add_action('wp_head', 'mourning_mourning');

function mourning_control()
{
	echo '<div class="wrap">';
	if (!empty($_POST)) {
		foreach ($_POST as $key => $val) {
			$val = trim($val);
			update_option($key, $val);
		}
		echo '<div class="notice is-dismissible updated" style="margin: 20px 0 10px 0; padding: .8em;">' . __('保存设置成功！', 'mourning') . '</div>';
	}
	$switch = get_option('mourning_switch');

?>
	<form class="controlform" method="post" action="#" style="background-color: #ffffff; border: 1px solid #cccccc; padding: 10px 30px;">
		<table class="form-table">
			<tr>
				<th scope="row"><?php _e('哀悼', 'mourning'); ?></th>
				<td>
					<select name="mourning_switch">
						<option value="off" <?php echo 'off' == $switch ? ' selected' : ''; ?>><?php _e('关闭', 'mourning'); ?></option>
						<option value="on" <?php echo 'on' == $switch ? ' selected' : ''; ?>><?php _e('开启', 'mourning'); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="mourning-start"><?php _e('开始时间', 'mourning'); ?></label></th>
				<td>
					<input id="mourning-start" name="mourning_start" class="regular-text code" type="text" value="<?php echo get_option('mourning_start', wp_date('Y-m-d')); ?>">
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="mourning-end"><?php _e('结束时间', 'mourning'); ?></label></th>
				<td>
					<input id="mourning-end" name="mourning_end" class="regular-text code" type="text" value="<?php echo get_option('mourning_end', wp_date('Y-m-d')); ?>">
				</td>
			</tr>
		</table>
		<p class="submit">
			<input type="submit" class="button-primary" value="<?php _e('保存设置', 'mourning'); ?>">
		</p>
	</form>
	</div>
<?php

}

//添加菜单
add_action('admin_menu', function () {
	add_options_page(__('哀悼', 'mourning'), __('哀悼', 'mourning'), 'administrator', 'mourning', 'mourning_control');
});

//插件页面设置链接
add_filter('plugin_action_links', function ($links, $file) {
	if ($file == plugin_basename(__FILE__)) {
		array_unshift($links, '<a href="options-general.php?page=mourning">' . __('设置', 'mourning') . '</a>');
	}
	return $links;
}, 10, 2);
?>